package DAO;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import hibernate.Role;
import hibernate.User;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import hibernateUtil.HibernateUtil;

public class RoleDAO {
	public void AddRole(String rolename,String rolememo,String message[]){
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		Role role = new Role();
		role.setName(rolename);
		role.setRdescribe(rolememo);
		//资产维护
		role.setrAssetCreate(Integer.parseInt(message[0]));
		role.setrAssetDelete(Integer.parseInt(message[1]));
		role.setrAssetUpdate(Integer.parseInt(message[2]));
		role.setrAssetRead(Integer.parseInt(message[3]));
		//资产报损
		role.setrAssetDamageCreate(Integer.parseInt(message[4]));
		role.setrAssetDamageDelete(Integer.parseInt(message[5]));
		role.setrAssetDamageUpdate(Integer.parseInt(message[6]));
		role.setrAssetDamageRead(Integer.parseInt(message[7]));
		//资产调拨
		role.setrAssetChangeCreate(Integer.parseInt(message[8]));
		role.setrAssetChangeDelete(Integer.parseInt(message[9]));
		role.setrAssetChangeUpdate(Integer.parseInt(message[10]));
		role.setrAssetChangeRead(Integer.parseInt(message[11]));
		//资产出售
		role.setrAssetSaleCreate(Integer.parseInt(message[12]));
		role.setrAssetSaleDelete(Integer.parseInt(message[13]));
		role.setrAssetSaleUpdate(Integer.parseInt(message[14]));
		role.setrAssetSaleRead(Integer.parseInt(message[15]));
		//资产维修
		role.setrAssetServiceCreate(Integer.parseInt(message[16]));
		role.setrAssetServiceDelete(Integer.parseInt(message[17]));
		role.setrAssetServiceUpdate(Integer.parseInt(message[18]));
		role.setrAssetServiceRead(Integer.parseInt(message[19]));
		// 资产报表查看
		role.setrOldReport(Integer.parseInt(message[20]));
		role.setrChangeReport(Integer.parseInt(message[21]));
		role.setrServiceReport(Integer.parseInt(message[22]));
		role.setrAssetReport(Integer.parseInt(message[23]));
		//购置申请
		role.setrPurchaseSupplyCreate(Integer.parseInt(message[24]));
		role.setrPurchaseSupplyDelete(Integer.parseInt(message[25]));
		role.setrPurchaseSupplyUpdate(Integer.parseInt(message[26]));
		role.setrPurchaseSupplyRead(Integer.parseInt(message[27]));
		//购置申请审批
		role.setrPurchaseSupplyApprove(Integer.parseInt(message[28]));
		//添加部门和资产类别
		role.setrAssetSortDictionary(Integer.parseInt(message[29]));
		role.setrDepartmentDictionary(Integer.parseInt(message[30]));
		//用户管理
		role.setrCreateUser(Integer.parseInt(message[31]));
		role.setrDeleteUser(Integer.parseInt(message[32]));
		role.setrUpdateUser(Integer.parseInt(message[33]));
		role.setrReadUser(Integer.parseInt(message[34]));
		//角色管理
		role.setrCreateRole(Integer.parseInt(message[35]));
		role.setrDeleteRole(Integer.parseInt(message[36]));
		role.setrUpdateRole(Integer.parseInt(message[37]));
		role.setrReadRole(Integer.parseInt(message[38]));
		
		session.save(role);
		trans.commit();
	}
	public Vector getrolename(){
		Vector v = new Vector();
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		Query query =session.createQuery("from Role");
		v.add(query.list());
		trans.commit();
		
		return v;
	}
	
	public Vector searchUser(String start,String limit,String rolename)
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		Vector vt = new Vector();
		
		String str = "from Role where name like '"+rolename+"'";
		Query query = session.createQuery(str);    //查询数据库
		
		int count = (query.list()).size();
		vt.add(count);
		
		query.setFirstResult(Integer.parseInt(start));    //查询记录从这一条开始；
		query.setMaxResults(Integer.parseInt(limit)); 	  //查询记录限制条数；

		List l = query.list();
		vt.add(l);
		trans.commit();
		
		return vt;
	}
	
	public Vector getAll(String start,String limit)
	{
		Vector vt = new Vector();
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		Query query = session.createQuery("from Role");
		int count = (query.list()).size();       //得到查询总数目；
		vt.add(count);
		
		if(Integer.parseInt(limit)!=-1){
		    query.setFirstResult(Integer.parseInt(start));    //查询记录从这一条开始；
		    query.setMaxResults(Integer.parseInt(limit)); 	  //查询记录限制条数；
		}
		
		List l = query.list();               //查询总数目中的limit条记录出来；
		vt.add(l);
		trans.commit();	
		return vt;
	}
	public void UpdateUser(String IdOfRole,String name,String rdescribe,String js){
		String[] JSMC = {"rAssetCreate","rAssetRead","rAssetUpdate","rAssetDelete","rAssetDamageCreate","rAssetDamageRead",
				         "rAssetDamageUpdate","rAssetDamageDelete","rAssetChangeCreate","rAssetChangeRead","rAssetChangeUpdate",
				         "rAssetChangeDelete","rAssetSaleCreate","rAssetSaleRead","rAssetSaleUpdate","rAssetSaleDelete",
				         "rAssetServiceCreate","rAssetServiceRead","rAssetServiceUpdate","rAssetServiceDelete","	rPurchaseSupplyCreate",
				         "rPurchaseSupplyRead","rPurchaseSupplyUpdate","rPurchaseSupplyDelete","rPurchaseSupplyApprove",
				         "rOldReport","rChangeReport","rServiceReport","rAssetReport","rCreateUser","rReadUser","rUpdateUser",
				         "rDeleteUser","rCreateRole","rReadRole","rUpdateRole","rDeleteRole","rAssetSortDictionary","rDepartmentDictionary"};

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		Vector v = new Vector();
		int startlength = 0,endlength=0,end;
		String id;
		if(js != ""){
			end=js.lastIndexOf(" ");
			startlength = js.indexOf(" ", 0);
			id = js.substring(0,startlength);
			v.add(id);
			if(startlength != end){
				while(endlength != end){
					endlength = js.indexOf(" ", startlength+1);
					id=js.substring(startlength+1, endlength);
					v.add(id);
					startlength = endlength;
				}
			}
		}
		
		String sql = "update Role set name='"+ name +"'"+", rdescribe='"+ rdescribe +"'";
		int num[] = new int[JSMC.length];
		for(int i=0;i<JSMC.length;i++){
			num[i]=0;
		}
		for(int i=0;i<v.size();i++){
			num[(Integer.parseInt((String)v.get(i))-1)]=1;
		}
		for(int i=0;i<JSMC.length;i++){
			if(i==0)
				sql += ",";
			sql += JSMC[i] +"='"+num[i]+"'";
			if(i!=(JSMC.length-1))
				sql += ",";
		}
		sql += " where roleID="+Integer.parseInt(IdOfRole);
		Query q = session.createQuery(sql);
		
		q.executeUpdate();
		trans.commit();
   }
	
	public void deleterole(String data){
		Vector v = new Vector();
		int startlength = 0,endlength=0,end;
		String id;
	    end=data.lastIndexOf(" ");
		if(startlength != end){
			System.out.println("..............................");
			while(endlength != end){
				endlength = data.indexOf(" ", startlength+1);
				id=data.substring(startlength+1, endlength);
				v.add(id);
				startlength = endlength;
			}
		}
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		User user;
		Role role;
		for(int i=0;i<v.size();i++){
			List l = session.createCriteria(Role.class).add(Restrictions.like("roleID",Integer.parseInt((String)v.get(i)))).list();
			role = (Role)l.get(0);
			Set<User> sr = role.getUsers();
		    Iterator sit = sr.iterator();
		    for(;sit.hasNext();)
		    {
		    	user = (User)sit.next();
		    	sit.remove();
		    	role.getUsers().remove(user);
		    }
		    session.save(role);
		}
		trans.commit();
		
		Session session2 = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans2 = session2.beginTransaction();
		String sql = "delete Role where ";
		for(int i=0;i<v.size();i++){
			sql += "roleID="+(String)v.get(i);
			if(i!=v.size()-1)
				sql += " or ";
		}
		if(v.size()>0){
			Query q = session2.createQuery(sql);
			q.executeUpdate();
		}
		trans2.commit();
	}
	
	public Vector select(int userID){
		Vector v = new Vector();
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		User user;
		Role role;
		List l = session.createCriteria(User.class).add(Restrictions.like("userID",userID)).list();
		user = (User)l.get(0);
		Set<Role> sr = user.getRoles();
	    Iterator sit = sr.iterator();
	    for(;sit.hasNext();)
	    {
	    	role = (Role)sit.next();
	    	v.add(role.getRoleID());
	    	v.add(role.getName());
	    }
		trans.commit();
		return v;
	}
}
